#!/usr/bin/env python 
# -*- coding: utf-8 -*-
from socket import *
from time import ctime 

HOST = "localhost"
PORT = 21567
BUFSIZE = 1024
ADDR = (HOST, PORT)

tcpSerSock = socket(AF_INET, SOCK_STREAM)
tcpSerSock.bind(ADDR)
tcpSerSock.listen(5)

while True:
 print("waiting for connect...")
 tcpCliSock, addr = tcpSerSock.accept()
 #连接套接字描述符  发起connect的主机地址
 print '...connected from:', addr

 while True:
	data = tcpCliSock.recv(BUFSIZE)
	if not data:
		break
	
	print "client",addr,"data : ",data
	tcpCliSock.send("[%s] %s" % (ctime(), data))
	#after sending the data , then write the log
	time_data = ctime() + " " + data
	file = open("/root/share/python/server_recv.log","a")
	#file.write("recv:")
	#file.write(time_data)
	#file.write("\n")
	inputTimeData = "recv: \n" + time_data + "\n"
	file.write(inputTimeData)
	file.close()
	if data == "end":
		break #when the client send the "end" close the connect soecket
		#waiting for another client send the connect request
 tcpCliSock.close()

tcpSerSock.close()